package com.homegoods.login.presenter;

import android.databinding.ObservableField;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.homegoods.login.ApiUtil;
import com.homegoods.login.KGetVerHelper;
import com.homegoods.login.R;
import com.homegoods.login.view.RestPWDView;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

/**
 * 找回密码Presenter
 *
 * @author QiQi
 * @date 2017/8/9
 */

public class ResetPwdPresenter extends BasePresenter<RestPWDView> {
    /**
     * 手机号
     */
    public final ObservableField<String> strPhoneNumber = new ObservableField<>();
    /**
     * 密码
     */
    public final ObservableField<String> strPwd = new ObservableField<>();
    /**
     * 验证码
     */
    public final ObservableField<String> strVer = new ObservableField<>();

    private final KGetVerHelper kGetVerHelper;

//    private String smsId;

    public ResetPwdPresenter(RestPWDView view, KGetVerHelper kGetVerHelper) {
        super(view);
        this.kGetVerHelper = kGetVerHelper;
    }

    /**
     * 下一步
     *
     * @param view
     */
    public void onNextClicked(View view) {
        if (checkRegisterData(view)) {
            reset(view);
        }
    }

    /**
     * 检查登录所需数据的合法性
     *
     * @param view
     * @return
     */
    private boolean checkRegisterData(View view) {
        String phone = strPhoneNumber.get();
        String pwd = strPwd.get();
        String verCode = strVer.get();

        //手机号不能为空
        if (!StringUtil.isPhoneNum(phone)) {
            ToastUtil.showSnackbar(view, R.string.string_hint_phone);
            return false;
        }

        if (!StringUtil.checkPassword(pwd)) {
            ToastUtil.showSnackbar(view, "请输入6-16位数字，字母");
            return false;
        }
        if (TextUtils.isEmpty(verCode)) {
            ToastUtil.showSnackbar(view, R.string.input_ver_code);
            return false;
        }
        if (verCode.length() < 5) {
            ToastUtil.showSnackbar(view, "请输入5位验证码");
            return false;
        }


        return true;
    }

    private void reset(final View view) {
        this.mView.hideKeyBoard();
        String phone = strPhoneNumber.get();
        String pwd = strPwd.get();
        String verCoder = strVer.get();
        mView.showLoad(view.getContext().getString(R.string.loading));
        // 重设密码接口
        Map<String, Object> params = new HashMap<>(3);
//        params.put("sid", smsId);
        params.put("authCode", verCoder);
        params.put("telephone", phone);
        params.put("password", /*MyUtils.MD5(*/pwd/*)*/);
        ApiUtil.forgetPassWord(params, jsonObject -> {
            mView.hideLoad();
            ToastUtil.showSnackbar(view, "密码重置成功，请重新登录");
            mView.resetSuc();
        }, throwable -> {
            mView.hideLoad();
            ToastUtil.showSnackbar(view, "密码重置失败");
        });
    }

    /**
     * 获取验证码
     *
     * @param view
     */
    public void onGetVerClickListener(View view) {
        if (checkGetVerCodeData()) {
            getVerCode();
        }
    }

    /**
     * 检查获取验证码是数据的合法性
     *
     * @return
     */
    private boolean checkGetVerCodeData() {
        String phone = strPhoneNumber.get();
        if (!StringUtil.isPhoneNum(phone)) {
            ToastUtil.showToast(R.string.string_hint_phone);
            return false;
        }

        return true;
    }

    private void getVerCode() {
        // 获取验证码接口
        ApiUtil.getCaptchaForForgetPassWord(strPhoneNumber.get(), new Consumer<JSONObject>() {
            @Override
            public void accept(JSONObject jsonElement) throws Exception {
                kGetVerHelper.sendSuc();
//            smsId = jsonElement.getString("sId");
            }
        }, throwable -> kGetVerHelper.resetCount());
    }
}
